<?php

namespace App\Http\Controllers\Admin;

use App\Models\BackLogData;
use App\Models\BucketCategory;
use App\Models\BucketData;
use App\Models\BucketLog;
use App\Models\Picture;
use App\Models\Shop;

/**
 * 商品管理控制器
 */
class BucketController extends BaseController
{

    public function __construct(
        BucketLog $bucketLog,
        Shop $shop,
        BucketCategory $bucketCategory,
        BucketData $bucketData,
        BackLogData $backLogData
    )
    {
        parent::__construct();
        $this->bucketLog = $bucketLog;
        $this->shop = $shop;
        $this->bucketCategory = $bucketCategory;
        $this->bucketData = $bucketData;
        $this->backLogData = $backLogData;
    }


    public function getIndex()
    {
        $shopList = $this->shop->_getList(['admin_id' => is_login()])->toArray();
        $rows = $this->bucketData->getOwnShopDataList(request('shop_id'));
        $category = $this->bucketCategory->_getList(['admin_id' => is_login()], '*', ['sort' => 'asc'])->toArray();
        if (request()->has('shop_id')) {
            foreach ($shopList as $v) {
                if ($v['id'] == request('shop_id')) {
                    $curentShop = $v;
                }
            }
        }
        return view('admin.bucket.index', compact('rows', 'shopList', 'curentShop', 'category'));
    }

    public function getInfo()
    {
        $info = $this->bucketLog->_getOneById(request('id'));
        return view('admin.bucket.info', compact('info'));
    }

    /* 新增分类 */
    public function getAdd()
    {
//        $category = $this->bucketCategory->getCache();
        $category = $this->bucketCategory->_getList(['admin_id' => is_login(), 'status' => 1], '*', ['sort' => 'asc'])->toArray();
        $shop = $this->shop->_getOneById(request('shop_id'));
        $shop = $shop ? $shop->toArray() : [];
        return view('admin.bucket.edit', compact('category', 'shop'));
    }

    public function postAdd()
    {
        $res = $this->bucketData->addOrUpdate(request('shop_id'));
        if (!$res) {
            return $this->error($this->bucketData->getError() ? $this->bucketData->getError() : '未知错误');
        }
        return $this->success('新增成功', urlAdmin('bucket', 'getIndex', ['shop_id' => request('shop_id')]));
    }


    /* 编辑分类 */
    public function getEdit()
    {
//        $category = $this->bucketCategory->getCache();
        $category = $this->bucketCategory->_getList(['admin_id' => is_login(), 'status' => 1], '*', ['sort' => 'asc'])->toArray();
        $info = $this->bucketData->_getJionOne('shop', ['id' => request('id')]);
        $info = $info ? $info->toArray() : [];
        return view('admin.bucket.edit', compact('category', 'info'));
    }

    public function postEdit()
    {
        $res = $this->bucketData->addOrUpdate();
        if (!$res) {
            return $this->error($this->bucketData->getError() ? $this->bucketData->getError() : '未知错误');
        }
        return $this->success('更新成功', urlAdmin('bucket', 'getIndex', ['shop_id' => request('shop_id')]));
    }

    public function getLogList()
    {
        $shop = $this->shop->_redisGetOneBySid(request('shop_id'));
        $rows = $this->backLogData->getLogList(request('shop_id'), request('bucket_category_id'));
        return view('admin.bucket.log_list', compact('rows', 'shop'));
    }

    public function getAddLog()
    {
        $category = $this->bucketCategory->getCache();
        $shop = $this->shop->_getOneById(request('shop_id'));
        $shop = $shop ? $shop->toArray() : [];
        return view('admin.bucket.edit', compact('category', 'shop'));
    }

    public function postAddLog()
    {
        $res = $this->bucketLog->addOrUpdate();
        if (!$res) {
            return $this->error($this->bucketLog->getError() ? $this->bucketLog->getError() : '未知错误');
        }
        return $this->success('新增成功', urlAdmin('bucket', 'getIndex', ['shop_id' => request('shop_id')]));
    }

    /* 编辑分类 */
    public function getEditLog()
    {
        $category = $this->bucketCategory->getCache();
        $info = $this->bucketLog->_getOneById(request('id'));
        $info = $info ? $info->toArray() : [];
        return view('admin.bucket.edit', compact('category', 'info'));
    }

    public function postEditLog()
    {
        $res = $this->bucketLog->addOrUpdate();
        if (!$res) {
            return $this->error($this->bucketLog->getError() ? $this->bucketLog->getError() : '未知错误');
        }
        return $this->success('更新成功', urlAdmin('bucket', 'getIndex', ['shop_id' => request('shop_id')]));
    }

    /**
     * 管理员状态修改
     */
    public function anyChangeStatus()
    {
        $method = request('method');
        $ids = array_unique((array)request('ids'));
        if (empty($ids)) {
            return $this->error('请选择要操作的数据!');
        }
        if (!is_array($ids)) {
            $ids = [$ids];
        }
        $where['id'] = ['in', $ids];
        switch (strtolower($method)) {
            case 'up':
                return $this->resume($this->bucketLog, $where, ['success' => '上架成功！', 'error' => '上架失败！']);
            case 'down':
                return $this->delete($this->bucketLog, $where, ['success' => '下架成功！', 'error' => '下架失败！']);
            default:
                return $this->error('参数非法');
        }
    }

}
